/*
 * RemoteFileProperties.java
 *
 * Created on 21/12/2011, 21:51:57
 */

package br.mg.bhe.ftpobre.gui.fs;

import org.apache.commons.net.ftp.FTPFile;

import br.mg.bhe.ftpobre.fs.RemoteFile;
import br.mg.bhe.ftpobre.util.DateConverter;
import br.mg.bhe.ftpobre.util.FileSizeConverter;
import br.mg.bhe.ftpobre.util.RemoteFileUtil;

/**
 *
 * @author Aureliano
 */
public class RemoteFilePropertiesFrame extends javax.swing.JDialog {

    /** Creates new form RemoteFileProperties */
    public RemoteFilePropertiesFrame(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        RemoteFile file = this.getSelectedRemoteFile();
		this.loadData(file);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelRawListing = new javax.swing.JLabel();
        textFieldRawListing = new javax.swing.JTextField();
        labelName = new javax.swing.JLabel();
        labelNameValue = new javax.swing.JLabel();
        labelGroup = new javax.swing.JLabel();
        labelGroupValue = new javax.swing.JLabel();
        labelLink = new javax.swing.JLabel();
        labelLinkValue = new javax.swing.JLabel();
        labelUser = new javax.swing.JLabel();
        labelUserValue = new javax.swing.JLabel();
        labelSize = new javax.swing.JLabel();
        labelSizeValue = new javax.swing.JLabel();
        labelDate = new javax.swing.JLabel();
        labelDateValue = new javax.swing.JLabel();
        labelType = new javax.swing.JLabel();
        labelTypeValue = new javax.swing.JLabel();
        labelFolder = new javax.swing.JLabel();
        labelFolderValue = new javax.swing.JLabel();
        labelFile = new javax.swing.JLabel();
        labelFileValue = new javax.swing.JLabel();
        labelSymbolicLink = new javax.swing.JLabel();
        labelSymbolicLinkValue = new javax.swing.JLabel();
        labelUnknown = new javax.swing.JLabel();
        labelUnknownValue = new javax.swing.JLabel();
        ButtonConfirm = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Propriedades");
        setResizable(false);

        labelRawListing.setText("Saída:");

        textFieldRawListing.setEditable(false);

        labelName.setText("Nome:");

        labelNameValue.setText("...");

        labelGroup.setText("Grupo:");

        labelGroupValue.setText("...");

        labelLink.setText("Link:");

        labelLinkValue.setText("...");

        labelUser.setText("Usuário:");

        labelUserValue.setText("...");

        labelSize.setText("Tamanho:");

        labelSizeValue.setText("...");

        labelDate.setText("Data:");

        labelDateValue.setText("...");

        labelType.setText("Tipo:");

        labelTypeValue.setText("...");

        labelFolder.setText("Pasta:");

        labelFolderValue.setText("...");

        labelFile.setText("Arquivo:");

        labelFileValue.setText("...");

        labelSymbolicLink.setText("Link simbólico:");

        labelSymbolicLinkValue.setText("...");

        labelUnknown.setText("Desconhecido:");

        labelUnknownValue.setText("...");

        ButtonConfirm.setText("OK");
        ButtonConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonConfirmActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelRawListing)
                        .addGap(76, 76, 76)
                        .addComponent(textFieldRawListing, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelName)
                            .addComponent(labelGroup)
                            .addComponent(labelLink)
                            .addComponent(labelUser)
                            .addComponent(labelSize)
                            .addComponent(labelDate)
                            .addComponent(labelType)
                            .addComponent(labelUnknown)
                            .addComponent(labelSymbolicLink)
                            .addComponent(labelFile)
                            .addComponent(labelFolder))
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelFolderValue)
                            .addComponent(labelFileValue)
                            .addComponent(labelUnknownValue)
                            .addComponent(labelSymbolicLinkValue)
                            .addComponent(labelTypeValue)
                            .addComponent(labelDateValue)
                            .addComponent(labelSizeValue)
                            .addComponent(labelUserValue)
                            .addComponent(labelLinkValue)
                            .addComponent(labelGroupValue)
                            .addComponent(labelNameValue)))
                    .addComponent(ButtonConfirm, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelRawListing)
                    .addComponent(textFieldRawListing, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelName)
                    .addComponent(labelNameValue))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelGroup)
                    .addComponent(labelGroupValue))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelLink)
                    .addComponent(labelLinkValue))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelUser)
                    .addComponent(labelUserValue))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSize)
                    .addComponent(labelSizeValue))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDate)
                    .addComponent(labelDateValue))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelType)
                    .addComponent(labelTypeValue))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelFolder)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labelFile)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labelSymbolicLink)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labelUnknown))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelFolderValue)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labelFileValue)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labelSymbolicLinkValue)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labelUnknownValue)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                .addComponent(ButtonConfirm)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-505)/2, (screenSize.height-427)/2, 505, 427);
    }// </editor-fold>//GEN-END:initComponents

    private void ButtonConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonConfirmActionPerformed
        super.dispose();
    }//GEN-LAST:event_ButtonConfirmActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonConfirm;
    private javax.swing.JLabel labelDate;
    private javax.swing.JLabel labelDateValue;
    private javax.swing.JLabel labelFile;
    private javax.swing.JLabel labelFileValue;
    private javax.swing.JLabel labelFolder;
    private javax.swing.JLabel labelFolderValue;
    private javax.swing.JLabel labelGroup;
    private javax.swing.JLabel labelGroupValue;
    private javax.swing.JLabel labelLink;
    private javax.swing.JLabel labelLinkValue;
    private javax.swing.JLabel labelName;
    private javax.swing.JLabel labelNameValue;
    private javax.swing.JLabel labelRawListing;
    private javax.swing.JLabel labelSize;
    private javax.swing.JLabel labelSizeValue;
    private javax.swing.JLabel labelSymbolicLink;
    private javax.swing.JLabel labelSymbolicLinkValue;
    private javax.swing.JLabel labelType;
    private javax.swing.JLabel labelTypeValue;
    private javax.swing.JLabel labelUnknown;
    private javax.swing.JLabel labelUnknownValue;
    private javax.swing.JLabel labelUser;
    private javax.swing.JLabel labelUserValue;
    private javax.swing.JTextField textFieldRawListing;
    // End of variables declaration//GEN-END:variables
	
	private void loadData(RemoteFile file) {
		FTPFile f = file.getFile();
		textFieldRawListing.setText(f.getRawListing());
		labelNameValue.setText(f.getName());
		labelGroupValue.setText(f.getGroup());
		labelLinkValue.setText((f.getLink() == null) ? "..." : f.getLink());
		labelUserValue.setText(f.getUser());
		labelSizeValue.setText(FileSizeConverter.convertFileSize(f.getSize()));
		labelDateValue.setText(DateConverter.convert(f.getTimestamp().getTimeInMillis()));
                labelTypeValue.setText(RemoteFileUtil.getFileType(f.getType()));
		labelFolderValue.setText((f.isDirectory()) ? "Sim" : "Não");
		labelFileValue.setText((f.isFile()) ? "Sim" : "Não");
		labelSymbolicLinkValue.setText((f.isSymbolicLink()) ? "Sim" : "Não");
		labelUnknownValue.setText((f.isUnknown()) ? "Sim" : "Não");
	}
	
	private RemoteFile getSelectedRemoteFile() {
		RemoteFile file = RemoteFileUtil.getSelectedFile();
		if (file == null) {
			throw new RuntimeException(
					"Não foi possível recuperar o arquivo selecionado da relação de arquivos remotos.");
		}
		
		return file;
	}
}